package com.glxt.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

@Configuration
public class MyLocalResovel implements LocaleResolver {

    // 自定义区域解析方式
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        // 获取页面手动切换传递的语言参数lang
        String lang = httpServletRequest.getParameter("lang");
        Locale locale = null;
        // 如果手动切换参数不为空，就根据手动参数进行语言切换
        if (!StringUtils.isEmpty(lang)) {
            try {
                String[] split = lang.split("_");
                locale = new Locale(split[0], split[1]);
            } catch (Exception e) {
                // 处理解析异常
                e.printStackTrace();
            }
        }
        if (locale == null) {
            // 获取请求头自动传递的语言参数Accept-Language
            String header = httpServletRequest.getHeader("Accept-Language");
            if (!StringUtils.isEmpty(header)) {
                try {
                    String[] splits = header.split(",");
                    String[] split = splits[0].split("-");
                    locale = new Locale(split[0], split[1]);
                } catch (Exception e) {
                    // 处理解析异常
                    e.printStackTrace();
                }
            }
        }
        // 如果都解析失败，使用默认Locale
        if (locale == null) {
            locale = Locale.getDefault();
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, @Nullable HttpServletResponse response, @Nullable Locale locale) {
        // 可以在这里实现设置Locale的逻辑
    }

    // 将自定义的MyLocalResovel类重新注册为一个类型LocaleResolver的Bean组件
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocalResovel();
    }
}